package com.sgb.kjwl.view.ui.activity.power;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCommonListBinding;
import com.sgb.kjwl.di.power.DaggerPowerComponent;
import com.sgb.kjwl.di.power.PowerModule;
import com.sgb.kjwl.model.entity.MaterialFilterEntity;
import com.sgb.kjwl.model.entity.PowerEntity;
import com.sgb.kjwl.model.entity.PowerRequestEntity;
import com.sgb.kjwl.view.ui.adapter.power.PowerNewAdapter;
import com.sgb.kjwl.view.widget.PowerFliterPopWindow;
import com.sgb.kjwl.viewmodel.power.PowerListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class PowerListActivity extends AppActivity {

    @Inject
    PowerListModel model;
    private String id;
    private ActivityCommonListBinding binding;
    private PowerNewAdapter adapter;
    private PowerFliterPopWindow popWindow;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPowerComponent.builder().appComponent(appComponent).powerModule(new PowerModule(this)).build().inject(this);
        id = getIntent().getStringExtra("id");
        model.filterJson.get().setCategoryNo(id);
        model.filterJson.get().setAreaNo("1");
        model.filterJson.get().setTypeNo("");
        model.filterJson.get().setLevelNo("");
        model.filterJson.get().setRecordNo("");
    }

    public static void start(Context context, String id, String title) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.putExtra("title", title);
        intent.setClass(context, PowerListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_common_list);
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setText("筛选");
        binding.include.titleView.setText(getIntent().getStringExtra("title"));
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setOnClickListener(this);

        adapter = new PowerNewAdapter(getActivity(), null);
        binding.list.setLayoutManager(new LinearLayoutManager(this));
        binding.list.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<PowerEntity>() {
            @Override
            public void onItemClick(PowerEntity powerEntity, int position) {
                PowerActivity.start(getActivity(), powerEntity.getNo());
            }
        });

        binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                model.getList(false);
            }

            @Override
            public void onLoadMore() {
                model.getList(true);
            }
        });

        popWindow = new PowerFliterPopWindow(this);
//        popWindow.setBgView(binding.grayLayout);
        popWindow.setFilterResultClick(new PowerFliterPopWindow.FilterResultClick() {
            @Override
            public void result(PowerRequestEntity filterJson) {
                model.filterJson.get().setTypeNo(filterJson.getTypeNo());
                model.filterJson.get().setLevelNo(filterJson.getLevelNo());
                model.filterJson.get().setRecordNo(filterJson.getRecordNo());
                model.filterJson.get().setAreaNo(filterJson.getAreaNo());
                model.getList(false);
            }
        });

    }

    @Override
    protected void init() {
        model.getList(false);
        model.getTypeFilter();
        model.getRecordFilter();
        model.getAreaFilter();
    }

    public void showList(List<PowerEntity> list, boolean isLoadMore) {
        binding.list.loadMoreComplete();
        binding.list.refreshComplete();
        if (isLoadMore) {
            adapter.addDatas(list);
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            adapter.setDatas(list);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.right_view:
                popWindow.showPopwindow(binding.include.rightView);
                break;
        }
    }

    public void setTypeFilter(List<MaterialFilterEntity> data) {
        popWindow.setTypeLevelData(data);
    }

    public void setRecordFilter(List<MaterialFilterEntity> data) {
        popWindow.setRecordData(data);
    }

    public void setAreaFilter(List<MaterialFilterEntity> data) {
        popWindow.setAreaData(data);
    }
}
